
package com.yimixian.app.rest.api;

import android.text.TextUtils;

import com.yimixian.app.cart.CartManager;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.common.SharePreferenceKeys;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.goods.Good;
import com.yimixian.app.model.Address;
import com.yimixian.app.model.GoodDetail;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.model.ImageInfo;
import com.yimixian.app.model.PromotionInfo;
import com.yimixian.app.model.SearchKeyResult;
import com.yimixian.app.util.SharedPreferencesHelper;

import org.json.JSONArray;
import org.json.JSONObject;

import retrofit.Callback;
import retrofit.client.Response;

import java.util.ArrayList;
import java.util.List;

public class GoodDetailAPI extends YmxBaseAPI<GoodDetail> {
    public static final String GOOD_DETAIL = "/v4/goods/{id}/show";
    private String mGoodId = "";

    public GoodDetailAPI(String goodId, Callback<Response> cb) {
        super(cb);
        mGoodId = goodId;
    }

    @Override
    public String getRelativeURL() {
        return GOOD_DETAIL;
    }

    public void getGoodDetail() {
        Address address = SharedPreferencesHelper.getAddress(SharePreferenceKeys.CURRENT_ADDRESS);
        if(address != null && !TextUtils.isEmpty(address.storeId)) {
            int storeId = Integer.parseInt(address.storeId);
            if(storeId > 0) {
                String token = DataManager.getInstance().get(CacheKeys.TOKEN);
                if(TextUtils.isEmpty(token)) {
                    mYmxRestService.getGoodDetailInfo(mGoodId,storeId, mCallback);
                } else {
                    mYmxRestService.getGoodDetailInfo(mGoodId, storeId,token, mCallback);
                }
            }else {
                String token = DataManager.getInstance().get(CacheKeys.TOKEN);
                if (TextUtils.isEmpty(token)) {
                    mYmxRestService.getGoodDetailInfo(mGoodId, mCallback);
                } else {
                    mYmxRestService.getGoodDetailInfo(mGoodId, token, mCallback);
                }
            }
        } else {
            String token = DataManager.getInstance().get(CacheKeys.TOKEN);
            if (TextUtils.isEmpty(token)) {
                mYmxRestService.getGoodDetailInfo(mGoodId, mCallback);
            } else {
                mYmxRestService.getGoodDetailInfo(mGoodId, token, mCallback);
            }
        }
    }

    @Override
    public GoodDetail parseJson(JSONObject jsonObject) {
        GoodDetail goodDetail = new GoodDetail();
        GoodDetail.Share share = new GoodDetail.Share();
        Good good = new Good();
        goodDetail.good = good;
        goodDetail.share = share;
        if (jsonObject != null) {
            JSONObject detailObject = jsonObject.optJSONObject("good_detail");
            if (detailObject != null) {
                JSONObject goodObject = detailObject.optJSONObject("good");
                if (goodObject != null) {
                    goodDetail.good.mGoodsItem = new GoodsItem();
                    goodDetail.good.id = goodObject.optString("id");
                    goodDetail.good.title = goodObject.optString("title");
                    goodDetail.good.unitDesc = goodObject.optString("unit_desc");
                    goodDetail.good.productby = goodObject.optString("productby");
                    goodDetail.good.unitPrice = goodObject.optString("unit_price");
                    goodDetail.good.unitOldPrice = goodObject.optString("unit_old_price");
                    goodDetail.good.stockCount = goodObject.optString("stock_count");
                    goodDetail.good.saleStatus = goodObject.optString("sale_status");
                    goodDetail.good.newPromoteType = goodObject.optString("new_promo_type");
                    goodDetail.good.promoType = goodObject.optString("promo_type");
                    goodDetail.good.funtionDesc = goodObject.optString("funtion_desc");
                    goodDetail.good.functionRatio = goodObject.optDouble("function_ratio");
                    goodDetail.good.funtionImgurl = goodObject.optString("funtion_imgurl");
                    //解析mGoodsItem:
                    goodDetail.good.mGoodsItem.id = Integer.parseInt(goodObject.optString("id"));
                    goodDetail.good.mGoodsItem.name = goodObject.optString("name");
                    goodDetail.good.mGoodsItem.imgRatio = goodObject.optInt("img_ratio");
                    goodDetail.good.mGoodsItem.promotionImg = goodObject.optString("promotion_img");
                    goodDetail.good.mGoodsItem.saleStatus = goodObject.optString("sale_status");
                    goodDetail.good.mGoodsItem.stockCount = goodObject.optString("stock_count");
                    goodDetail.good.mGoodsItem.saleStatus = goodObject.optString("sale_status");
                    goodDetail.good.mGoodsItem.unitPrice = goodObject.optString("unit_price");
                    goodDetail.good.mGoodsItem.unitOldPrice = goodObject.optString("unit_old_price");
                    goodDetail.good.mGoodsItem.shouldBuyCount = goodObject.optInt("should_buy_count");
                    goodDetail.good.mGoodsItem.isBuyingGroup = goodObject.optInt("is_buying_group");
                    goodDetail.good.mGoodsItem.buyingGroupCount = goodObject.optInt("buying_group_count");
                    goodDetail.good.mGoodsItem.countZeroReason = goodObject.optString("count_zero_reason");
                    goodDetail.good.mGoodsItem.countZeroReason = goodObject.optString("count_zero_reason");
                     JSONArray imageInfoArray = goodObject.optJSONArray("function_new_imgurl");
                    List<ImageInfo> list = new ArrayList<>();
                    if(imageInfoArray != null) {
                        for(int i=0;i<imageInfoArray.length();i++) {
                            JSONObject imageInfoObj = imageInfoArray.optJSONObject(i);
                            if(imageInfoObj != null) {
                                ImageInfo info = new ImageInfo();
                                info.image = imageInfoObj.optString("url");
                                info.image_ratio = (float)imageInfoObj.optDouble("ratio");
                                info.title = goodDetail.good.funtionDesc;
                                list.add(info);
                            }
                        }
                    }
                    goodDetail.good.mImageInfos = list;
                    goodDetail.good.mGoodsItem.buyingGroupPrice = (float) goodObject
                        .optDouble("buying_group_price");
                    JSONArray urlArray = goodObject.optJSONArray("img_url");
                    if (urlArray != null) {
                        List<String> imgList = new ArrayList<>();
                        for (int i = 0; i < urlArray.length(); i++) {
                            String object = (String) urlArray.opt(i);
                            imgList.add(object);
                        }
                        goodDetail.good.imgUrl = new ArrayList<>();
                        goodDetail.good.imgUrl.clear();
                        goodDetail.good.imgUrl.addAll(imgList);
                    }

                    //2.3.0商品促销信息
                    JSONArray infos = goodObject.optJSONArray("promotion_info");
                    if(infos != null){
                        goodDetail.good.mGoodsItem.promotionInfos = new ArrayList<>();
                        for (int i = 0; i < infos.length(); i++) {
                            PromotionInfo info = new PromotionInfo();
                            info.title = infos.optJSONObject(i).optString("title");
                            if(info.title != null && info.title.length() > 2){
                                info.title = info.title.substring(0, 2);
                            }
                            info.desc = infos.optJSONObject(i).optString("desc");
                            info.url = infos.optJSONObject(i).optString("url");
                            goodDetail.good.mGoodsItem.promotionInfos.add(info);
                        }
                    }

                }
                goodDetail.good.goodsLevel = detailObject.optInt("goods_level");
                JSONObject shareObject = detailObject.optJSONObject("share");
                if (shareObject != null) {
                    goodDetail.share.title = shareObject.optString("title");
                    goodDetail.share.iconImgurl = shareObject.optString("icon_imgurl");
                    goodDetail.share.shareurl = shareObject.optString("shareurl");
                    goodDetail.share.shareContent = shareObject.optString("share_content");
                }
                goodDetail.canComment = detailObject.optBoolean("can_comment");
                goodDetail.commentShow = detailObject.optBoolean("comment_show");
                goodDetail.commentBtnName = detailObject.optString("comment_btn_name");
                goodDetail.tasteDesc = detailObject.optString("taste_desc");
                goodDetail.freshnessDesc = detailObject.optString("freshness_desc");
                //更新GoodItems的数据源
                if(goodDetail != null && goodDetail.good != null && goodDetail.good.mGoodsItem != null) {
                    if(CartManager.getInstance().mIdToGoodsItem.get(goodDetail.good.mGoodsItem.id) == null) {
                        CartManager.getInstance().mIdToGoodsItem.put(goodDetail.good.mGoodsItem.id, goodDetail.good.mGoodsItem);
                    } else {
                        if(goodDetail != null && goodDetail.good != null && goodDetail.good.mGoodsItem != null) {
                            CartManager.getInstance().mIdToGoodsItem.get(goodDetail.good.mGoodsItem.id).shouldBuyCount = goodDetail.good.mGoodsItem.shouldBuyCount;
                        }
                    }
                }
            }
        }
        return goodDetail;
    }

}
